/**
 * Created by admin on 14-8-6.
 */
if(typeof (module) !="undefined"){
    module.exports = FMatrix;
}


function FMatrix (){
    this.m = [
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]
    ];
}

// JSON.stringify会回调此函数
FMatrix.prototype.toJSON = function(){
    var ret = {};
    ret.m =  [  this.m[0][0],this.m[0][1],this.m[0][2],
                this.m[1][0],this.m[1][1],this.m[1][2],
                this.m[2][0],this.m[2][1],this.m[2][2]  ];
    return ret;
}

// 转成json字符串
FMatrix.prototype.toJson = function() {
    var ret  = '{';

    ret +='"m":[';
    for(var i=0;i<9;i++){
        if(i>0){
            ret +=',';
        }
        ret += this.m[Math.floor(i/3)][i%3];
    }

    ret +=']';

    ret +='}';
    return ret;
}
// 从json对象中解析
FMatrix.prototype.fromJson = function(jsonObj){
    if(jsonObj.hasOwnProperty("m") &&  jsonObj.m!=null && jsonObj.m.length==9){
        for(var i=0;i<9;i++){
            this.m[Math.floor(i/3)][i%3] = jsonObj.m[i];
        }
    }
}

FMatrix.prototype.createFromFMatrix = function(m){
    if(m==null)	return null;
    
    var ret = new FMatrix();
    ret.m[0][0] = m.m[0][0];
    ret.m[0][1] = m.m[0][1];
    ret.m[0][2] = m.m[0][2];
    ret.m[1][0] = m.m[1][0];
    ret.m[1][1] = m.m[1][1];
    ret.m[1][2] = m.m[1][2];
    ret.m[2][0] = m.m[2][0];
    ret.m[2][1] = m.m[2][1];
    ret.m[2][2] = m.m[2][2];

    return ret;
}
// 矩阵乘法
FMatrix.prototype.multiply = function(matrix){
    if(!matrix) return this ;

    var m00 = this.m[0][0]*matrix.m[0][0] + this.m[0][1]*matrix.m[1][0] + this.m[0][2]* matrix.m[2][0];
    var m01 = this.m[0][0]*matrix.m[0][1] + this.m[0][1]*matrix.m[1][1] + this.m[0][2]* matrix.m[2][1];
    var m02 = this.m[0][0]*matrix.m[0][2] + this.m[0][1]*matrix.m[1][2] + this.m[0][2]* matrix.m[2][2];

    var m10 = this.m[1][0]*matrix.m[0][0] + this.m[1][1]*matrix.m[1][0] + this.m[1][2]* matrix.m[2][0];
    var m11 = this.m[1][0]*matrix.m[0][1] + this.m[1][1]*matrix.m[1][1] + this.m[1][2]* matrix.m[2][1];
    var m12 = this.m[1][0]*matrix.m[0][2] + this.m[1][1]*matrix.m[1][2] + this.m[1][2]* matrix.m[2][2];

    var m20 = this.m[2][0]*matrix.m[0][0] + this.m[2][1]*matrix.m[1][0] + this.m[2][2]* matrix.m[2][0];
    var m21 = this.m[2][0]*matrix.m[0][1] + this.m[2][1]*matrix.m[1][1] + this.m[2][2]* matrix.m[2][1];
    var m22 = this.m[2][0]*matrix.m[0][2] + this.m[2][1]*matrix.m[1][2] + this.m[2][2]* matrix.m[2][2];

    this.m[0][0] = m00;
    this.m[0][1] = m01;
    this.m[0][2] = m02;

    this.m[1][0] = m10;
    this.m[1][1] = m11;
    this.m[1][2] = m12;

    this.m[2][0] = m20;
    this.m[2][1] = m21;
    this.m[2][2] = m22;

    return this;
}
FMatrix.prototype.createMatrix = function(){
    var ret = new FMatrix();
    return ret;
}
FMatrix.prototype.initRotateMatrix = function(angle,x,y){
    var ret = new FMatrix();
    ret.m = [
        [Math.cos(angle), Math.sin(angle), 0],
        [-Math.sin(angle), Math.cos(angle), 0],
        [(1 - Math.cos(angle)) * x + y * Math.sin(angle), (1 - Math.cos(angle)) * y - x * Math.sin(angle), 1]
    ];
    return ret;
}

//1*3矩阵相乘
FMatrix.prototype.multiplyPoint = function(x,y){
    var point = [x,y,1];
    var sum = 0;
    console.log(this.m);
    var change = new Array();
    for(var i=0; i<3; i++){
        change.push(point[0]*this.m[0][i]+ point[1]*this.m[1][i] + point[2]*this.m[2][i]  );
    }
    return change;
}






